package com.luo;
import java.util.Scanner;

/**
 * @Author: luosiyu
 * @Description: 综合性实验-猜姓氏
 * @Version: 1.0
 */
public class GuessSurname {
    public static void main(String[] args) {
        //定义姓氏的字符串数组
        String[] firstNames={" ","张","王","李","赵","刘","于","许","金","钱","孙"};
        //                    0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,
        //                      4    3   34   2    24   23  234   1    14    13
        //                      1    2   3     4    5   6     7    8    9    10
        //通过二进制数据的规律将十个姓氏放在不同组里
        //将姓氏字符串加入卡牌分析：2进制数的四位分别代表卡牌的1234，通过进制转换，将字符按对应位置加入到卡牌
        String[] groups = {"","","",""};
        //将10进制数转换成2进制
        //通过转换规律，将字符放入对应的索引字符串内
        for (int i = 0; i < firstNames.length; i++) {
        int num =i;
        int index=groups.length-1;//4
            while(num!=0) {//利用十进制转二进制除2法
                int a = num % 2;
                num = num / 2;
                if (a == 1) {
                    groups[index] += firstNames[i];
                }
            index--;
            }
        }
        //遍历卡牌字符数组，打印
        for (int i = 0; i < groups.length; i++) {
            System.out.println(i+1+":"+groups[i]);
        }
        System.out.println("请输入你所想的姓氏所在位置:(以-1表示结束):");
        //通过输入的字符来添加二进制的1数位置，输入-1结束
        char[] c={'0','0','0','0'};//输入
        Scanner scanner = new Scanner(System.in);
        while (true){
            int i = scanner.nextInt();
            if (i == -1) {
                break;
            }
            c[i-1] = '1';
        }
        //将char数组转换成字符串再用parseInt将2进制数转换成十进制姓氏数组下标，获取到用户所想字符
        System.out.println("你的姓氏为：\n"+firstNames[Integer.parseInt(new String(c), 2)]);
    }
}
